package com.cms.system.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cms.lang.util.QueryParams;
import com.cms.system.dao.UserDAO;
import com.cms.system.entity.UserEntity;

/**
 * @ClassName: SystemLogService
 * @Description: 系统用户业务逻辑接口
 * @author WangJia
 * @date 2011-9-27
 * @version v1.0
 */
@Service
public class UserService {

	@Autowired
	private UserDAO userDAO;

	/**
	 * 
	 * @Description: 概述：删除用户<br/>
	 * @MethodName delete
	 * @author WangJia
	 * @date 2013-1-5 上午10:01:21
	 * @version v1.0
	 * @param id
	 * @throws DAOException
	 */
	public void delete(String id) {
		UserEntity entity = this.findById(id);
		this.delete(entity);

	}

	/**
	 * 
	 * @Description: 概述：删除用户<br/>
	 *               步骤：<br/>
	 *               1、删除用户<br/>
	 *               2、添加操作日志<br/>
	 * @MethodName delete
	 * @author WangJia
	 * @date 2013-1-5 上午10:01:38
	 * @version v1.0
	 * @param entity
	 * @throws DAOException
	 */

	public void delete(UserEntity entity) {
		this.userDAO.delete(entity);
	}

	/**
	 * 
	 * @Description: 概述：根据ID查询用户<br/>
	 * @MethodName findById
	 * @author WangJia
	 * @date 2013-1-5 上午10:01:58
	 * @version v1.0
	 * @param id
	 * @return
	 * @throws DAOException
	 */

	public UserEntity findById(String id) {
		UserEntity u = userDAO.findOne(id);
		return u;
	}

	/**
	 * 
	 * @Description: 概述：通过用户注册名查询用户<br/>
	 * @MethodName findByLoginName
	 * @author WangJia
	 * @date 2013-1-5 上午10:02:17
	 * @version v1.0
	 * @param loginName
	 * @return
	 * @throws DAOException
	 */
	public UserEntity findByLoginName(String loginName) {

		return userDAO.findByLoginName(loginName);
	}

	/**
	 * 
	 * @Description: 概述：添加用户<br/>
	 *               步骤：<br/>
	 *               1、添加用户<br/>
	 *               2、添加操作日志<br/>
	 * @MethodName insert
	 * @author WangJia
	 * @date 2013-1-5 上午10:02:31
	 * @version v1.0
	 * @param entity
	 * @throws DAOException
	 */

	public void insert(UserEntity entity) {
		this.userDAO.save(entity);
	}

	/**
	 * 
	 * @Description: 概述：查询所有用户<br/>
	 * @MethodName listAll
	 * @author WangJia
	 * @date 2013-1-5 上午10:02:55
	 * @version v1.0
	 * @return
	 * @throws common.db.DAOException
	 */
	public List<UserEntity> listAll() {
		return (List<UserEntity>) this.userDAO.findAll();
	}

	/**
	 * 
	 * @Description: 概述：修改用户信息<br/>
	 *               步骤：<br/>
	 *               1、修改用户信息<br/>
	 *               2、添加操作日志<br/>
	 * @MethodName update
	 * @author WangJia
	 * @date 2013-1-5 上午10:00:41
	 * @version v1.0
	 * @param entity
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws DAOException
	 */
	@Transactional
	public void update(UserEntity entity) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
		
		updateNotNullField(db, entity);
	}

	/**
	 * 
	 * @Description: 概述：修改用户登录信息 <br/>
	 *               步骤：<br/>
	 *               1、修改用户登录信息 <br/>
	 *               2、添加登录日志<br/>
	 * @MethodName updateLoginInfo
	 * @author WangJia
	 * @date 2013-1-4 下午9:44:27
	 * @version v1.0
	 * @param entity
	 * @param lastLoginIp
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws DAOException
	 */

	public void updateLoginInfo(UserEntity entity, String lastLoginIp) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
		// 修改登录信息
		entity.setLastLoginIp(lastLoginIp);
		if (StringUtils.isEmpty(entity.getLoginCounts())) {
			entity.setLoginCounts(entity.getLoginCounts() + 1);
		} else {
			entity.setLoginCounts(1);
		}
		entity.setLastLoginTime(new Date());
		this.update(entity);
		// 添加登录日志日志
	}

	public boolean validateName(String loginName) {
		boolean success = false;
		if (StringUtils.isEmpty(this.findByLoginName(loginName))) {
			success = true;
		}
		return success;
	}

	

	/**
	 * *******************************
	 * 
	 * @Description: 概述：TODO<br/>
	 * @MethodName findByQuery
	 * @author WangJia
	 * @date 2017年12月19日 下午4:48:57
	 * @version v1.0
	 * @param q
	 * @param pageable
	 * @return
	 *********************************
	 */
	@SuppressWarnings("unchecked")
	public List<UserEntity> findByQuery(@SuppressWarnings("rawtypes") QueryParams params, Pageable pageable) {
		return userDAO.findByQueryParam(params, pageable);
	}

	public List<UserEntity> findAll() {
		return userDAO.findAll();
	}

	public List<UserEntity> findAll(String... id) {
		return userDAO.findAll(id);
	}
}
